package com.tanhua.server.controller;

import com.tanhua.commons.utils.JwtUtils;
import com.tanhua.dubbo.api.UserApi;
import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.vo.ErrorResult;
import com.tanhua.model.vo.UserInfoVo;
import com.tanhua.server.exception.BusinessException;
import com.tanhua.server.interceptor.UserHolder;
import com.tanhua.server.service.UserInfoService;
import com.tanhua.server.service.UserService;
import io.jsonwebtoken.Claims;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/users")
public class UsersController {
    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private UserService userService;
    @DubboReference
    private UserApi userApi;

    /**
     * 查看用户详细信息
     *
     * @param token
     * @param userID
     * @return
     */
    @GetMapping
    public ResponseEntity users(@RequestHeader("Authorization") String token, Long userID) {

        if (userID == null) {
            //如果userid为空，从线程中获取用户id，在进行赋值userid
            userID = UserHolder.getUserId();
        }

        UserInfoVo userInfo = userInfoService.finfById(userID);
        return ResponseEntity.ok(userInfo);
    }

    /**
     * 修改用户信息
     *
     * @param token
     * @param userInfo
     * @return
     */
    @PutMapping
    public ResponseEntity updateUserInfo(@RequestHeader("Authorization") String token, @RequestBody UserInfo userInfo) {
        //设置id 从UserHolder线程中获取用户id
        userInfo.setId(UserHolder.getUserId());
        userInfoService.update(userInfo);
        return ResponseEntity.ok(null);
    }

    /**
     * 更新头像
     *
     * @param token
     * @param headPhoto
     * @return
     * @throws IOException
     */
    @PostMapping("/header")
    public ResponseEntity updateHeader(@RequestHeader("Authorization") String token, MultipartFile headPhoto) throws IOException {
        //从UserHolder线程中获取用户id
        userInfoService.updateHead(headPhoto, UserHolder.getUserId());
        return ResponseEntity.ok(null);
    }

    /**
     * 修改手机号，发送验证码
     *
     * @return
     */
    @PostMapping("/phone/sendVerificationCode")
    public ResponseEntity sendVerificationCode(@RequestHeader("Authorization") String token) {
        //从线程中获取当前用户的手机号
        String mobile = UserHolder.get().getMobile();
        //通过手机号发送验证码
        userService.sendMsg(mobile);
        System.err.println("验证码发送成功！");
        return ResponseEntity.ok(null);
    }

    /**
     * 校验验证码
     *
     * @param map
     * @return
     */
    @PostMapping("/phone/checkVerificationCode")
    public ResponseEntity checkVerificationCode(@RequestBody Map map) {
        String mobile = UserHolder.get().getMobile();
        String code = (String) map.get("verificationCode");
        //如果验证码正确就返回true给flag
        Boolean resultCode = userService.checkVerificationCode(mobile, code);
        System.err.println(resultCode);
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("verification", resultCode);
        return ResponseEntity.ok(hashMap);
    }


    /**
     * 更换手机号
     *
     * @param map
     * @return
     */
    @PostMapping("/phone")
    public ResponseEntity phone(@RequestBody Map map) {
        String phone = (String) map.get("phone");
        userService.getPhone(phone);
        return ResponseEntity.ok(null);
    }

    /**
     * 互相喜欢，喜欢，粉丝 - 统计
     * @return
     */
    @GetMapping("/counts")
    public ResponseEntity counts() {
        Map map = userService.counts();
        return ResponseEntity.ok(map);
    }
}
